import wx from 'weixin-js-sdk';
var that
export default {
    data () {
        return this.datainit({
            order: {
                goods_list: []
            },
            address: null
        })
    },

    mounted: function () {
        this.pre_token = this.$route.query.pre_token
        that = this
        that.address = that.$cookies.get("address")
        this.check()
        that.$post('api/index/getjssdk', {
            url: window.location.href.toString(),
            apilist: 'openAddress|chooseWXPay'
        }, function (data) {
            wx.config(data);
        })
    },
    methods: {
        user_init: function (_user) {
            that.$post("api/order/get_pre", {pre_token: this.pre_token}, function (data) {
                that.order = data
            })
        },
        setaddress() {
            wx.openAddress({
                success: function (res) {
                    let data = {}
                    data.addressee = res.userName
                    data.country = res.countryName
                    data.province = res.provinceName
                    data.city = res.cityName
                    data.tel = res.telNumber
                    data.addressdetail = res.detailInfo
                    that.$cookies.set("address", data)
                    that.address = data
                }
            });
        },
        buy(){
            that.$post("api/order/make", {pre_token: that.pre_token, address: that.address}, function (orderno) {
                that.$post("api/order/pay", {orderno: orderno}, function (config) {
                    config.success = function (res) {
                        that.$toast("支付成功")
                        setTimeout(function () {
                            that.$router.push({path:'/pages/setting/index'});
                        },2000)
                    }
                    wx.chooseWXPay(config);
                })
            })
        }

    },
}